package dongnv.poweramp.data;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import dongnv.poweramp.activity.HomeActivity;

public class SystemManager {
	public SystemManager() {
		this.isPlay = false;
		createMediaPlayer();
		loadAllFolder(Environment.getExternalStorageDirectory().getPath() + "/");
		scanAllSong(new File(Environment.getExternalStorageDirectory().getPath() + "/"));

		this.currentSongItem = arraySongItem.get(0);
		try {
			mediaPlayer.setDataSource(currentSongItem.getPath());
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void createMediaPlayer() {
		this.mediaPlayer = new MediaPlayer();
	}

	public void removeMediaPlayer() {
		this.mediaPlayer = null;
	}

	public void loadAllFolder(String path) {
		if (path != null) {
			File directory = new File(path);
			if (directory != null) {
				File[] listFiles = directory.listFiles();
				if (listFiles != null && listFiles.length > 0) {
					for (File file : listFiles) {
						if (file.isDirectory()) {
							addFolderMusic(file);
							loadAllFolder(file.getAbsolutePath());
						}
					}
				}
			}
		}
	}

	public void addFolderMusic(File directory) {
		int countSongInFolder = 0;
		if (directory != null) {
			File[] listFiles = directory.listFiles();
			if (listFiles != null && listFiles.length > 0) {
				for (File file : listFiles) {
					if (!file.isDirectory()) {
						
						if (file.getName().endsWith(".mp3") || file.getName().endsWith(".mp4") 
								|| file.getName().endsWith(".flv")) {
							countSongInFolder++;
						}
					} 
				}
			}
		}
		if (countSongInFolder > 0) {
			FolderItem folderItem = new FolderItem(directory.getName(), directory.getAbsolutePath(), countSongInFolder);
			arrayFolderItem.add(folderItem);
			updateListFolderAdapter();
		}
	}

	public void scanAllSong(File directory) {
		if (directory != null) {
			File[] listFiles = directory.listFiles();
			if (listFiles != null && listFiles.length > 0) {
				for (File file : listFiles) {
					if (file.isDirectory()) {
						scanAllSong(file);
					} else {
						addSong(file);
					}
				}
			}
		}
	}

	public void scanSong(File directory) {
		if (directory != null) {
			File[] listFiles = directory.listFiles();
			if (listFiles != null && listFiles.length > 0) {
				for (File file : listFiles) {
					if (!file.isDirectory()) {
						addSong(file);
					}
				}
			}
		}
	}

	public void addSong(File song) {
		if (song.getName().endsWith(".mp3") || song.getName().endsWith(".mp4") || song.getName().endsWith(".flv")) {
			String name = song.getName().substring(0, (song.getName().length() - 4));
			String path = song.getPath();
			SongItem songItem = new SongItem(name, path);
			arraySongItem.add(songItem);
			updateListSongPlayAdapter();
		}
	}

	public boolean isPlay() {
		return isPlay;
	}

	public void setPlay(boolean isPlay) {
		this.isPlay = isPlay;
	}

	public ArrayList<SongItem> getArraySongItem() {
		return arraySongItem;
	}

	public ListSongAdapter getListSongAdapter() {
		return listSongAdapter;
	}

	public void setListSongAdapter(ListSongAdapter listSongAdapter) {
		this.listSongAdapter = listSongAdapter;
	}

	public void updateListFolderAdapter() {
		if (listFolderAdapter != null) {
			listFolderAdapter.notifyDataSetChanged();
			listFolderAdapter.notifyDataSetInvalidated();
		}
	}

	public void updateListSongPlayAdapter() {
		if (listSongAdapter != null) {
			Log.e("", "update");
			listSongAdapter.notifyDataSetChanged();
			listSongAdapter.notifyDataSetInvalidated();
		}
	}

	public Handler getMessageHandler() {
		return messageHandlerHome;
	}

	public void setMessageHandler(Handler messageHandler) {
		this.messageHandlerHome = messageHandler;
	}

	public Handler getMessageHandlerListSong() {
		return messageHandlerListSong;
	}

	public void setMessageHandlerListSong(Handler messageHandlerListSong) {
		this.messageHandlerListSong = messageHandlerListSong;
	}

	public SongItem getCurrentSongItem() {
		return currentSongItem;
	}

	public void setCurrentSongItem(SongItem currentSongItem) {
		this.currentSongItem = currentSongItem;
	}

	public MediaPlayer getMediaPlayer() {
		if (mediaPlayer != null) {
			return mediaPlayer;
		} else {
			return new MediaPlayer();
		}
	}

	public void setMediaPlayer(MediaPlayer mediaPlayer) {
		this.mediaPlayer = mediaPlayer;
	}

	public FolderItem getCurrentFolderItem() {
		return currentFolderItem;
	}

	public void setCurrentFolderItem(FolderItem currentFolderItem) {
		this.currentFolderItem = currentFolderItem;
	}

	public ArrayList<FolderItem> getArrayFolderItem() {
		return arrayFolderItem;
	}

	public void addArrayFolderItem(FolderItem folderItem) {
		arrayFolderItem.add(folderItem);
	}

	public Handler getMessageHandlerListFolder() {
		return messageHandlerListFolder;
	}

	public void setMessageHandlerListFolder(Handler messageHandlerListFolder) {
		this.messageHandlerListFolder = messageHandlerListFolder;
	}

	public ListFolderAdapter getListFolderAdapter() {
		return listFolderAdapter;
	}

	public void setListFolderAdapter(ListFolderAdapter listFolderAdapter) {
		this.listFolderAdapter = listFolderAdapter;
	}

	private boolean isPlay;

	private ArrayList<SongItem> arraySongItem = new ArrayList<SongItem>();
	private SongItem currentSongItem;
	private ListSongAdapter listSongAdapter;

	private ArrayList<FolderItem> arrayFolderItem = new ArrayList<FolderItem>();
	private FolderItem currentFolderItem;
	private ListFolderAdapter listFolderAdapter;

	private Handler messageHandlerHome;
	private Handler messageHandlerListSong;
	private Handler messageHandlerListFolder;

	private MediaPlayer mediaPlayer;
}
